package com.cungmua.app.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cungmua.app.R;
import com.cungmua.app.model.Deal;
import com.cungmua.app.utils.AppUtils;
import com.koushikdutta.urlimageviewhelper.UrlImageViewCallback;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kelvin.nguyen on 6/17/14.
 */
public class ListDealAdapter extends BaseAdapter {

    private final Activity context;
    private final LayoutInflater inflater;
    private List<Deal> dataSource;


    public ListDealAdapter(Activity context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.dataSource = new ArrayList<Deal>();
    }

    @Override
    public int getCount() {
        return dataSource.size();
    }

    @Override
    public Deal getItem(int i) {
        return dataSource.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public List<Deal> getDataSource() {
        return dataSource;
    }

    public void addToDataSource(List<Deal> newPage) {
        this.dataSource.addAll(newPage);
        notifyDataSetChanged();
    }
    public void setDataSource(List<Deal> dataSource) {
        this.dataSource = dataSource;
        notifyDataSetChanged();
    }

    private class ViewHolder {
        public TextView txtShortName, txtDiscountPercent, txtMarketPrice,
                txtPrice, txtTotalRegistry;
        public ImageView imgImage;
        private LinearLayout imgQuantity;
    }

    /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     *
     * @param position    The position of the item within the adapter's data set of the item whose view
     *                    we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *                    is non-null and of an appropriate type before using. If it is not possible to convert
     *                    this view to display the correct data, this method can create a new view.
     *                    Heterogeneous lists can specify their number of view types, so that this View is
     *                    always of the right type (see {@link #getViewTypeCount()} and
     *                    {@link #getItemViewType(int)}).
     * @param parent      The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Deal deal = dataSource.get(position);

        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.list_items_deal, parent, false);

            holder.imgImage = (ImageView) convertView.findViewById(R.id.imgImage);
            holder.txtShortName = (TextView) convertView.findViewById(R.id.txtShortName);
            holder.txtDiscountPercent = (TextView) convertView.findViewById(R.id.txtDiscountPercent);
            holder.txtMarketPrice = (TextView) convertView.findViewById(R.id.txtMarketPrice);
            holder.txtMarketPrice.setPaintFlags(holder.txtMarketPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            holder.txtPrice = (TextView) convertView.findViewById(R.id.txtPrice);
            holder.txtTotalRegistry = (TextView) convertView.findViewById(R.id.txtTotalRegistry);
            holder.imgQuantity = (LinearLayout) convertView.findViewById(R.id.imageView2);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }



       // UrlImageViewHelper.setUrlDrawable(holder.imgImage, deal.getImageUrl());
        UrlImageViewHelper.setUrlDrawable(holder.imgImage,deal.getImageUrl(),new UrlImageViewCallback() {
            @Override
            public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {

                if(loadedBitmap!=null){
                    int width = loadedBitmap.getWidth();
                    int height = loadedBitmap.getHeight();
                    int screen_width=232;
                    Display display = context.getWindowManager().getDefaultDisplay();
                    Point size = new Point();
                    display.getSize(size);
                    screen_width = size.x;

                    if(height>=width){

                        imageView.getLayoutParams().height = screen_width;
                        imageView.getLayoutParams().width = screen_width;
                    }else{
                        imageView.getLayoutParams().height = 232;
                        imageView.getLayoutParams().width = screen_width;
                    }
                }

            }
        });


        holder.txtShortName.setText(deal.getShortName());

        String discountPercent=deal.getMarketPrice()==deal.getPrice()?context.getResources().getString(R.string.label_good_price):deal.getDiscountPercent()+"%";

        holder.txtDiscountPercent.setText("" + discountPercent);

        String priceString = context.getString(R.string.full_price_string, AppUtils.getFullPriceString(deal.getMarketPrice()));
        priceString=deal.getMarketPrice()==deal.getPrice()?"":priceString;
        holder.txtMarketPrice.setText(priceString);

        priceString = context.getString(R.string.full_price_string, AppUtils.getFullPriceString(deal.getPrice()));
        holder.txtPrice.setText(priceString);

        if (deal.getTotalRegistry() > 0) {
            holder.txtTotalRegistry.setText(String.valueOf(deal.getTotalRegistry()));
            holder.imgQuantity.setVisibility(View.VISIBLE);
            holder.txtTotalRegistry.setVisibility(View.VISIBLE);
        }else {
            holder.imgQuantity.setVisibility(View.INVISIBLE
            );
            holder.txtTotalRegistry.setVisibility(View.INVISIBLE);
        }


        return convertView;

    }
}
